﻿using System;
using System.Collections.Generic;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Bo;
using Monkey.Warrenty.Constants;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.InstantTickets
{
    public partial class Print : System.Web.UI.Page
    {
        #region Properties

        private InstantTicketBiz _instantTicketBiz;
        private InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        private long _ticketId;
        private long TicketId
        {
            get
            {
                if (_ticketId <= 0)
                {
                    long.TryParse(Request.Params["id"], out _ticketId);
                }
                return _ticketId;
            }
        }

        protected List<TaskServiceBo> TaskServices;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.AdminDichVu))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if (!Page.IsPostBack)
            {
                if (TicketId > 0)
                {
                    var instantTicketForPrint = InstantTicketManager.GetInstantTicketForPrint(TicketId);
                    if (instantTicketForPrint != null && instantTicketForPrint.Count > 0)
                    {

                        var instantTicketPrint = instantTicketForPrint[0];

                        ltrCode.Text = instantTicketPrint.InstantTicketCode;
                        ltrTicketDate.Text = string.Format("{0:dd/MM/yyyy HH:mm}", instantTicketPrint.InstantTicketDate);
                        ltrCustomerName.Text = instantTicketPrint.CustomerName;
                        ltrCustomerPhone.Text = instantTicketPrint.CustomerPhone;
                        ltrCustomerAddress.Text = instantTicketPrint.CustomerAddress;
                        ltrDescription.Text = instantTicketPrint.InstantTicketContent;
                    }
                }
                
                ltrDate.Text = string.Format("{0:dd/MM/yyyy HH:mm:ss}", DateTime.Now);
                TaskServices = InstantTicketManager.GetInstantTicketTaskServiceDetails(TicketId) ??
                               new List<TaskServiceBo>();
            }
        }
    }
}